/**
 * Copyright (c) 2012, Camelot and/or its affiliates. All rights reserved.
 * Camelot PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.camelot.ccr.system.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.camelot.ccr.global.controller.BaseController;
import com.camelot.ccr.global.util.Pager;
import com.camelot.ccr.global.util.Utility;
import com.camelot.ccr.system.dao.DriverDao;
import com.camelot.ccr.system.dao.PictureDao;
import com.camelot.ccr.system.dao.TeamDao;
import com.camelot.ccr.system.enums.ModuleEnum;
import com.camelot.ccr.system.persistence.Driver;
import com.camelot.ccr.system.persistence.Picture;
import com.camelot.ccr.system.persistence.Team;

/**
 * <pre>
 * 车队信息Controller
 * </pre>
 * @author Liu Jun
 * @version V0.1, 2014-5-23 下午2:11:38
 */
@Controller
@RequestMapping("/manage/driver")
public class DriverController extends BaseController {

	@Resource
	private TeamDao teamDao;
	@Resource
	private DriverDao driverDao;
	@Resource
	private PictureDao pictureDao;
	/**
	 * 选手头像的地址
	 */
	public static final String headUrlPath="/upload/images/headphoto/";
	/**
	 * 选手的图文资讯
	 */
	public static final String driverTwzxPath = "/upload/images/drivertwzx";
	/**
	 * 选手的职业生涯存放目录
	 */
	public static final String driverZysyPath = "/upload/images/driverzysy";
	/**
	 * <pre>
	 * 查询车队信息
	 * </pre>
	 * @param matchTeam
	 * @param model
	 * @return 车队列表
	 * @author Liu Jun, 2014-5-23 下午5:36:47
	 */
	@RequestMapping("/list")
	public String list(Driver driver,ModelMap model,HttpServletRequest request){
		String curpage = request.getParameter("curpage")==null?"0":request.getParameter("curpage");
		String pageSize = request.getParameter("pageSize");//ToDo 暂时未用到  暂定每页显示100
		Pager<Driver> pager = driverDao.findPageList(Integer.parseInt(curpage),100 , driver);
		
		model.put("pager", pager);
		return "/manage/driver/driver_index";
	}
	/**
	 * <pre>
	 * <跳转到车手添加页面>
	 * </pre>
	 * @param driver
	 * @param model
	 * @return
	 * @author Liu Jun, 2014-5-23 下午11:22:23
	 */
	@RequestMapping("/add")
	public String add(Driver driver,ModelMap model){
		List<Team> teamList = teamDao.getAll();
		model.put("teamList", teamList);
		return "/manage/driver/driver_add";
	}
	
	/**
	 * <pre>
	 * <保存车手信息并上传头像>
	 * </pre>
	 * @param file
	 * @param request
	 * @return
	 * @throws Exception
	 * @author Liu Jun, 2014-5-24 上午8:10:52
	 */
	@RequestMapping("/save")
	public String save(@RequestParam("headPhoto")CommonsMultipartFile file,HttpServletRequest request) throws Exception{
		String driverName = request.getParameter("driverName");
		String birthday = request.getParameter("birthday");
		String birthplace = request.getParameter("birthplace");
		String nationality = request.getParameter("nationality");
		String height = request.getParameter("height");
		String carNo = request.getParameter("carNo");
		String teamId = request.getParameter("team");
		String path = request.getRealPath(headUrlPath);
		
		Driver d = new  Driver();
		d.setBirthday(Utility.convertStringToTimestamp(birthday, Utility.DATE_DEFAULT_PATTERN));
		d.setBirthplace(birthplace);
		d.setDriverName(driverName);
		d.setNationality(nationality);
		d.setHeight(Float.valueOf(height));
		d.setCarNo(carNo);
		Team t = new Team();
		t.setId(teamId);
		d.setTeam(t);
		d.setHeadPhoto(path);
		
		driverDao.saveDriver(d, file);
		return "redirect:list";
	}
	@RequestMapping("/twzx")
	public String twzx(String driverId,ModelMap model,HttpServletRequest request){
		//ToDo 暂时定100
		String curpage = request.getParameter("curpage")==null?"0":request.getParameter("curpage");
		String pageSize = request.getParameter("pageSize");//ToDo 暂时未用到  暂定每页显示100
		//设置参数
		Picture example = new Picture();
		example.setBusinessId(driverId);
		example.setMoudleType(ModuleEnum.DRIVER_TWZX.name());
		Pager<Picture> pager = pictureDao.findPageList(Integer.parseInt(curpage), 100, example);
		
		model.put("pager", pager);
		model.put("driverId", driverId);
		return "/manage/driver/driver_twzx";
	}
	/**
	 * <pre>
	 * <uploadify组件图文资讯批量上传>
	 * </pre>
	 * @param request
	 * @throws Exception
	 * @author Liu Jun, 2014-5-24 上午10:25:43
	 */
	@RequestMapping("/uploadTwzx")
	public String uploadTwzx(MultipartHttpServletRequest request) throws Exception{
		
		String businessId = request.getParameter("businessId");
		String path = request.getRealPath(driverTwzxPath);
		List<MultipartFile> files = request.getFiles("files"); 
		String imageUrl = "";
		if(Utility.isNotEmpty(files)){
			Picture p = null;
			for(int i=0;i<files.size();i++){
				imageUrl = Utility.uploadFile(files.get(i), path,driverTwzxPath);
				p = new Picture();
				p.setBusinessId(businessId);
				p.setMoudleType(ModuleEnum.DRIVER_TWZX.name());
				p.setImgUrl(imageUrl);
				pictureDao.save(p);
			}
		}
		return "redirect:twzx?driverId="+businessId;
	}
	
	@RequestMapping("/detail")
	public String detail(ModelMap model,HttpServletRequest request) throws Exception{
		String id = request.getParameter("id");
		Driver driver = driverDao.findById(id);
		model.put("driver", driver);
		return "/manage/driver/driver_detail";
	}
	/**
	 * <pre>
	 * <查询车手的职业生涯>
	 * </pre>
	 * @param driverId
	 * @param model
	 * @param request
	 * @return
	 * @author Liu Jun, 2014-5-24 下午9:53:23
	 */
	@RequestMapping("/zysy")
	public String zysy(String driverId,ModelMap model,HttpServletRequest request){
		//ToDo 暂时定100
		String curpage = request.getParameter("curpage")==null?"0":request.getParameter("curpage");
		String pageSize = request.getParameter("pageSize");//ToDo 暂时未用到  暂定每页显示100
		
		Driver driver = driverDao.get(driverId);
		
		//设置参数
		Picture example = new Picture();
		example.setBusinessId(driverId);
		example.setMoudleType(ModuleEnum.DRIVER_ZYSY.name());
		Pager<Picture> pager = pictureDao.findPageList(Integer.parseInt(curpage), 100, example);
		
		model.put("pager", pager);
		model.put("driver", driver);
		return "/manage/driver/driver_zysy";
	}
	/**
	 * <pre>
	 * <跳转到添加职业生涯页面>
	 * </pre>
	 * @param driverId
	 * @param model
	 * @return
	 * @author Liu Jun, 2014-5-24 下午10:09:16
	 */
	@RequestMapping("/addZysy")
	public String addZysy(String driverId,ModelMap model){
		Driver driver = driverDao.get(driverId);
		model.put("driver", driver);
		return "/manage/driver/driver_zysy_add";
	}
	
	/**
	 * <pre>
	 * <保存车手职业生涯并上传图片>
	 * </pre>
	 * @param file
	 * @param request
	 * @return
	 * @author Liu Jun, 2014-5-24 下午10:18:25
	 * @throws IOException 
	 * @throws IllegalStateException 
	 */
	@RequestMapping("/saveZysy")
	public String saveZysy(@RequestParam("zysyPic")CommonsMultipartFile file,HttpServletRequest request) throws IllegalStateException, IOException{
		String businessId = request.getParameter("businessId");
		String title = request.getParameter("title");
		String path = request.getRealPath(driverZysyPath);
		
		Picture p = new Picture();
		p.setBusinessId(businessId);
		p.setCreateTime(new Date());
		p.setMoudleType(ModuleEnum.DRIVER_ZYSY.name());
		p.setTitle(title);
		p.setImgUrl(path);
		
		pictureDao.savePicture(p, file);
		
		return "redirect:zysy?driverId="+businessId;
	}
}
